package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.GiftInfo;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.GiftInfoService;
import com.javasm.service.impl.GiftInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/gift/*")
public class GiftServlet extends BaseServlet {
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");

        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        Integer pageSize = 10;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String giftName = req.getParameter("giftName");

        GiftInfo inputGift = new GiftInfo();
        inputGift.setGiftName(giftName);

        GiftInfoService giftInfoService = new GiftInfoServiceImpl();

        Integer total = giftInfoService.countGifts(inputGift);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<GiftInfo> giftInfos = giftInfoService.listAllGift(inputGift, page, pageSize);

        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(giftInfos.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(giftInfos);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String giftName = req.getParameter("giftName");
        String giftPriceStr = req.getParameter("giftPrice");
        Double giftPrice = null;
        if(giftPriceStr!=null&&!"".equals(giftPriceStr)){
            giftPrice = Double.parseDouble(giftPriceStr);
        }
        String giftLogo = req.getParameter("giftLogo");
        String giftRatioStr = req.getParameter("giftRatio");
        Double giftRatio = null;
        if(giftRatioStr!=null&&!"".equals(giftRatioStr)){
            giftRatio = Double.parseDouble(giftRatioStr);
        }
        GiftInfo inputGift = new GiftInfo(null,giftName,giftLogo,giftPrice,giftRatio);

        GiftInfoService giftInfoService = new GiftInfoServiceImpl();
        Integer addResult = giftInfoService.addGift(inputGift);
        ReturnResult rrs = new ReturnResult();
        if(addResult>0){
            //添加成功

            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            //添加失败
            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String giftIdStr = req.getParameter("giftId");
        Integer giftId = null;
        if(giftIdStr!=null&&!"".equals(giftIdStr)){
            giftId = Integer.parseInt(giftIdStr);
        }
        GiftInfoService giftInfoService = new GiftInfoServiceImpl();
        Integer deleteResult = giftInfoService.deleteGiftById(giftId);
        ReturnResult rrs = new ReturnResult();
        if(deleteResult>0){
            //删除成功
            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            //删除失败
            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String giftIdStr = req.getParameter("giftId");
        Integer giftId = null;
        if(giftIdStr!=null&&!"".equals(giftIdStr)){
            giftId = Integer.parseInt(giftIdStr);
        }
        String giftName = req.getParameter("giftName");
        String giftPriceStr = req.getParameter("giftPrice");
        Double giftPrice = null;
        if(giftPriceStr!=null&&!"".equals(giftPriceStr)){
            giftPrice = Double.parseDouble(giftPriceStr);
        }
        String giftLogo = req.getParameter("giftLogo");
        String giftRatioStr = req.getParameter("giftRatio");
        Double giftRatio = null;
        if(giftRatioStr!=null&&!"".equals(giftRatioStr)){
            giftRatio = Double.parseDouble(giftRatioStr);
        }
        GiftInfo inputGift = new GiftInfo(giftId,giftName,giftLogo,giftPrice,giftRatio);
        GiftInfoService giftInfoService = new GiftInfoServiceImpl();
        Integer editResult = giftInfoService.editGift(inputGift);
        ReturnResult rrs = new ReturnResult();
        if(editResult>0){
            //修改成功
            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            //修改失败
            rrs.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            rrs.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

    public void getGiftById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String giftIdStr = req.getParameter("giftId");
        Integer giftId = null;
        if(giftIdStr!=null&&!"".equals(giftIdStr)){
            giftId = Integer.parseInt(giftIdStr);
        }
        GiftInfoService giftInfoService = new GiftInfoServiceImpl();
        GiftInfo giftInfo = giftInfoService.getGiftById(giftId);
        ReturnResult rrs = new ReturnResult();
        if(giftInfo!=null){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setReturnData(giftInfo);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
}
